package cn.ld.module.ai.dal.mapper.model;

import cn.ld.framework.common.enums.CommonStatusEnum;
import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.mybatis.core.mapper.BaseMapperX;
import cn.ld.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ld.module.ai.controller.admin.model.vo.chatRole.AiChatRolePageInVO;
import cn.ld.module.ai.dal.entity.model.AiChatRoleEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * AI 聊天角色 Mapper
 *
 * @author fansili
 */
@Mapper
public interface AiChatRoleMapper extends BaseMapperX<AiChatRoleEntity> {

    default PageResult<AiChatRoleEntity> selectPage(AiChatRolePageInVO inVO) {
        return selectPage(inVO, new LambdaQueryWrapperX<AiChatRoleEntity>()
                .likeIfPresent(AiChatRoleEntity::getName, inVO.getName())
                .eqIfPresent(AiChatRoleEntity::getCategory, inVO.getCategory())
                .eqIfPresent(AiChatRoleEntity::getPublicStatus, inVO.getPublicStatus())
                .orderByAsc(AiChatRoleEntity::getSort));
    }

    default PageResult<AiChatRoleEntity> selectPageByMy(AiChatRolePageInVO inVO, Long userId) {
        return selectPage(inVO, new LambdaQueryWrapperX<AiChatRoleEntity>()
                .likeIfPresent(AiChatRoleEntity::getName, inVO.getName())
                .eqIfPresent(AiChatRoleEntity::getCategory, inVO.getCategory())
                // 情况一：公开
                .eq(Boolean.TRUE.equals(inVO.getPublicStatus()), AiChatRoleEntity::getPublicStatus, inVO.getPublicStatus())
                // 情况二：私有
                .eq(Boolean.FALSE.equals(inVO.getPublicStatus()), AiChatRoleEntity::getUserId, userId)
                .eq(Boolean.FALSE.equals(inVO.getPublicStatus()), AiChatRoleEntity::getStatus, CommonStatusEnum.ENABLE.getStatus())
                .orderByAsc(AiChatRoleEntity::getSort));
    }

    default List<AiChatRoleEntity> selectListGroupByCategory(Integer status) {
        return selectList(new LambdaQueryWrapperX<AiChatRoleEntity>()
                .select(AiChatRoleEntity::getCategory)
                .eq(AiChatRoleEntity::getStatus, status)
                .groupBy(AiChatRoleEntity::getCategory));
    }

    default List<AiChatRoleEntity> selectListByName(String name) {
        return selectList(new LambdaQueryWrapperX<AiChatRoleEntity>()
                .likeIfPresent(AiChatRoleEntity::getName, name)
                .orderByAsc(AiChatRoleEntity::getSort));
    }

}
